package com.crimax.skor;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.springframework.util.NumberUtils;

import com.crimax.skor.db.DbUtility;
import com.crimax.skor.model.RgbObject;
import com.crimax.skor.model.SearchObject;
import com.crimax.skor.model.SkorObject;
import com.google.gson.Gson;

public class ColorService {
	
	private final SearchObject searchObject;
	
	public ColorService(String colorParam,String hogklackatParam, String pumpsParam, String sandalerParam, String stovlarParam,
			String partyParam, String maxParam, String minParam) {
		super();
		
		searchObject = new SearchObject(colorParam, hogklackatParam, pumpsParam, sandalerParam, stovlarParam, partyParam, maxParam, minParam);
		
	}


	@SuppressWarnings("unchecked")
	public String searchRGBColor() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException{
	
		//this.searchObject.printThis();
		
		DbUtility dbUtility = new DbUtility();
		Connection con = dbUtility.getConnection();
		List<SkorObject> skor;
		if(this.searchObject.getColorParam()!=null){
			RgbObject rgbObject = new RgbObject(this.searchObject.getColorParam());		
			skor = dbUtility.searchSkorByColor(rgbObject,this.searchObject, con);
		}else{
			skor = dbUtility.searchSkorByColor(null,this.searchObject, con);
		}
		con.close();
		
		
		String skoToJson = this.skoToJson(skor);
//		
//		System.out.println("Json");
//		System.out.println(skoToJson);
		
		return skoToJson;
	}

	
	private String skoToJson(List<SkorObject> skor){
		
		Gson gson = new Gson();
		
		String json = gson.toJson(skor);
		
		return json;
	}
}